package com.enlorenz.common.geo.gts;

import com.enlorenz.common.geo.gts.baidu.BaiduGtsSearcher;
import com.enlorenz.core.cache.oper.DefaultCacheOper;
import com.enlorenz.core.cache.oper.ICacheOper;
import com.enlorenz.core.cache.oper.IMethodCacheProxy;
import com.enlorenz.core.cache.oper.TimeCacheOper;
import com.enlorenz.util.TimeUtil;

/**
 * Gts缓存代理
 * @author Karma
 */
public class CacheSearcherProxy implements IGtsSearcher{
	private ICacheOper cacheOper=new DefaultCacheOper();
	private IMethodCacheProxy cacheExecutor;
	public void setCacheOper(ICacheOper cacheOper) {
		this.cacheOper = cacheOper;
		//设置代理执行者
		cacheExecutor=new IMethodCacheProxy() {
			@Override
			public Object execute(Object[] params) throws Exception {
				return CacheSearcherProxy.this.gtsSearcher.query((Double)params[0], (Double)params[1]);
			}

			@Override
			public boolean isParamAdaptCache(Object[] params) {
				return true;
			}
		};
		cacheOper.setCacheExecutor(cacheExecutor);
	}

	private IGtsSearcher gtsSearcher;
	
	public void setGtsSearcher(IGtsSearcher gtsSearcher) {
		this.gtsSearcher = gtsSearcher;
		
	}
	@Override
	public String query(Double lon, Double lat) throws Exception {
		return (String) cacheOper.execute(new Object[]{lon,lat});
	}
	
	public static void main(String[] args) throws Exception {
		CacheSearcherProxy csp=new CacheSearcherProxy();
		
		TimeCacheOper cacheOper=new TimeCacheOper();
		cacheOper.setOverTime(TimeUtil.SECOND_MILLI*10);
		
		csp.setCacheOper(cacheOper);
		BaiduGtsSearcher s=new BaiduGtsSearcher();
		csp.setGtsSearcher(s);
		
		long st=System.currentTimeMillis();
		for(int i=0;i<10;i++){
			String str=csp.query(108.939362,34.305058);
			System.out.println(str);
			Thread.sleep(TimeUtil.SECOND_MILLI*5);
		}
		long ed=System.currentTimeMillis();
		System.out.println((ed-st));
	}
	
	
}
